package br.ita.doacoes.view.campanha;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import br.ita.doacoes.core.cadastrodoacoes.dao.PessoaDAO;
import br.ita.doacoes.core.cadastrodoacoes.dao.impl.PessoaDAOImpl;
import br.ita.doacoes.domain.cadastrodoacoes.Pessoa;
import br.ita.doacoes.domain.campanha.Campanha;
import br.ita.doacoes.view.voluntarios.CadastroPessoaFisica;

public class JanelaPessoa extends JFrame implements ActionListener,MouseListener{
	
	private JanelaCampanha jan;
	private Campanha camp;
	private List<Pessoa> pes;  //  @jve:decl-index=0:
	private int ind;
	DefaultTableModel model;

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JButton buttonAdd = null;

	private JButton buttonSel = null;

	private JButton buttonLista = null;

	private JScrollPane scrollPessoa = null;

	private MyTable tablePessoa = null;
	
	void populateTabela(){
		model.setRowCount(0);
		for(Pessoa i:pes){
			Object obj[] = new Object[2];
			obj[0] = i.getNome();
			obj[1] = i.getE_mail();
			model.addRow(obj);
		}
	}

	/**
	 * This is the default constructor
	 */
	public JanelaPessoa(JanelaCampanha j,Campanha c) {
		super();
		jan = j;
		camp = c;
		ind = -1;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(467, 341);
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		this.setTitle("Pessoas");
		this.setVisible(true);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getButtonAdd(), null);
			jContentPane.add(getButtonSel(), null);
			jContentPane.add(getButtonLista(), null);
			jContentPane.add(getScrollPessoa(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes buttonAdd	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getButtonAdd() {
		if (buttonAdd == null) {
			buttonAdd = new JButton();
			buttonAdd.setBounds(new Rectangle(150, 255, 151, 50));
			buttonAdd.setText("Gerenciar Pessoas");
			buttonAdd.addActionListener(this);
		}
		return buttonAdd;
	}

	/**
	 * This method initializes buttonSel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getButtonSel() {
		if (buttonSel == null) {
			buttonSel = new JButton();
			buttonSel.setBounds(new Rectangle(16, 255, 125, 49));
			buttonSel.setText("Selecionar");
			buttonSel.addActionListener(this);
		}
		return buttonSel;
	}

	/**
	 * This method initializes buttonLista	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getButtonLista() {
		if (buttonLista == null) {
			buttonLista = new JButton();
			buttonLista.setBounds(new Rectangle(316, 255, 137, 50));
			buttonLista.setText("Listar");
			buttonLista.addActionListener(this);
		}
		return buttonLista;
	}

	/**
	 * This method initializes scrollPessoa	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getScrollPessoa() {
		if (scrollPessoa == null) {
			scrollPessoa = new JScrollPane();
			scrollPessoa.setBounds(new Rectangle(15, 30, 435, 210));
			scrollPessoa.setViewportView(getTablePessoa());
		}
		return scrollPessoa;
	}

	/**
	 * This method initializes tablePessoa	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getTablePessoa() {
		if (tablePessoa == null) {
			Object[][] obj = {};
			String[] title = {"Nome","e-mail"};
			model = new DefaultTableModel(obj,title);
			tablePessoa = new MyTable(model);
			tablePessoa.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
			tablePessoa.addMouseListener(this);
		}
		return tablePessoa;
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == buttonAdd){
			CadastroPessoaFisica cad = new CadastroPessoaFisica();
			JFrame jf = new JFrame();
			jf.add(cad);
			jf.setVisible(true);
			jf.setExtendedState(MAXIMIZED_BOTH);
		}
		if(e.getSource() == buttonSel){
			if(ind < 0) return;
			jan.psel.add(pes.get(ind));
		}
		if(e.getSource() == buttonLista){
			PessoaDAO dao = new PessoaDAOImpl(Pessoa.class);
			pes = dao.getList();
			System.out.println(pes.size());
			this.populateTabela();
		}
	}

	public void mouseClicked(MouseEvent e) {
		ind = tablePessoa.getSelectedRow();
	}

	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
